<template>
  <div id="consoleCPU" style="width: 100%; height: 100%; background: #FFFFFF; text-align: center">
    <div ref="consoleCPU" :style="{ width:'100%',height:'100%' }"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import moment from 'moment/moment';

export default {
  name: 'consoleCPU',
  data() {
    return {
      chart: null,
      option: {
        title: {
          show: true,
          text: 'CPU',
          left: 'center',
          top: 20
        },
        grid: {
          show: true,
          right: '30px',
          containLabel: true
        },
        xAxis: {
          type: 'time',
          max: 'dataMax',
          boundaryGap: false, // 尝试移除 boundaryGap 或者设置为 false
          axisLabel: {
            formatter: function (value) {
              return moment(value).format('HH:mm:ss');
            },
            showMaxLabel: true,
            interval: 'auto',
            rotate: 45
          }
        },
        yAxis: {
          type: 'value',
          min: 0,
          max: 1,
          splitNumber: 6,
          position: 'left',
          silent: true,
          axisLabel: {
            formatter: function (value) {
              return value * 100 + '%';
            }
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: (data) => {
            return moment(data[0].data[0]).format("HH:mm:ss") + "</br> "
                + data[0].marker + "使用：" + (data[0].data[1] * 100).toFixed(2) + "%";
          }
        },
        series: [{
          type: 'line',
          itemStyle: {
            color: '#409EFF'
          },
          areaStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {offset: 0, color: '#50a3f8'},
                {offset: 1, color: '#69b0fa'}
              ],
              global: false
            }
          },
          data: [],
        }]
      }
    };
  },
  created() {
  },
  mounted() {
    this.initChart();
    this.$nextTick(_ => {
      setTimeout(() => {
        this.chart.resize()
      }, 100)
    })
  },
  destroyed() {
  },
  methods: {
    setData: function (data) {
      this.option.series[0].data = data;
      this.chart.setOption(this.option);
    },
    initChart() {
      this.chart = markRaw(echarts.init(this.$refs.consoleCPU));
    }
  }
};
</script>
